﻿using MoviesWeb.BLL;
using MoviesWeb.Model;
using MoviesWeb.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Movie : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDataToRepeater();
        }
        CKFinder.FileBrowser fileBrowser = new CKFinder.FileBrowser();
        fileBrowser.BasePath = "../ckfinder/";
        fileBrowser.SetupCKEditor(CK_introduce_);

        BindDataToRepeater();
    }
    private void BindDataToRepeater()
    {
        Repeater1.DataSource = MoviesBLL.KeyWordGetMovies("");
        Repeater1.DataBind();
    }
    protected void Bt_find_Click(object sender, EventArgs e)
    {
        Repeater1.DataSource = MoviesBLL.KeyWordGetMovies(TB_key.Text.Trim());
        Repeater1.DataBind();
    }
    protected void Bt_delete_Click(object sender, EventArgs e)
    {
        var su = 0;
        var er = 0;
        foreach (RepeaterItem item in Repeater1.Items)
        {
            var del_id = (item.FindControl("CheckBoxid") as CheckBox);
            if (del_id.Checked)
            {
                if (MoviesBLL.DeleteMovie(Convert.ToInt32(del_id.Attributes["value"])))
                    su++;
                else
                    er++;
            }
        }
        BindDataToRepeater();
        Response.Write("<script>alert('删除成功" + su + "条,失败" + er + "条','删除提示')  </script>");
    }
    protected void Bt_present_Click1(object sender, EventArgs e)
    {
        var record = new Movies()
            {
                Mv_name = name.Text.Trim(),
                Mv_type = Dl_type.SelectedItem.Value.Trim(),
                Mv_click = Convert.ToInt32(clickcount.Text.Trim()),
                Mv_downaddress = path.Text.Trim(),
                Mv_headpic = pic_path.Text.Trim(),
                Mv_introducepic = introducepic.Text.Trim(),
                Mv_uptime = DateTime.Now,
                Mv_wordintroduce = introduce.Text.Trim()
            };
        if (hide_id.Value == "")
        {
            if (MoviesBLL.AddMovie(record))
            {
                Response.Write("<script>alert('添加成功!')  </script>");
              //  Response.Write("<script> ShowMessage('添加成功!','添加提示')</script>");
                BindDataToRepeater();
            }
            else
                Response.Write("<script>alert('登陆失败!')  </script>");
               // Response.Write("<script> ShowMessage('添加失败!','添加提示')</script>");
        }
        else
        {
            record.Mv_id = Convert.ToInt32(hide_id.Value);
            if (MoviesBLL.ChangeMovie(record))
            {
                Response.Write("<script>alert('修改成功!')  </script>");
                //Response.Write("<script> ShowMessage('修改成功!','修改提示')</script>");
                BindDataToRepeater();
            }
            else
                Response.Write("<script>alert('修改失败!')  </script>");
                //Response.Write("<script> ShowMessage('修改失败!','修改提示')</script>");
            hide_id.Value = "";
        }
    }
    protected void bt_Alertintroduce_Click(object sender, EventArgs e)
    {
        if (MoviesBLL.AlertIntroduce(CK_introduce_.Text, Convert.ToInt32(hide_introduceid.Value)))
        {
            Response.Write("<script>alert('修改简介信息成功!')  </script>");
            //Response.Write("<script> ShowMessage('修改简介信息成功!','修改提示')</script>");
            BindDataToRepeater();
        }
        else
            Response.Write("<script>alert('修改简介信息失败!')  </script>");
            //Response.Write("<script> ShowMessage('修改简介信息失败!','修改提示')</script>");
    }
}